<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
	<title>员工二维码</title>
	<link rel="stylesheet" type="text/css" href="css/public.css">
	<link rel="stylesheet" type="text/css" href="css/common.css">
	<style>
		.teamName{
			float: left;
		}
		.employeeName{
			float: right;
		}
		.productName{
			font-size: 1.5em;
			margin-top: 4em;
			width: 100%;
			text-align: center;
		}
	</style>
</head>
<body>
	<div class="cont">
		<span class="teamName"></span>
		<span class="employeeName"></span>
		<div class="productName"></div>
		<div id="qrcode"></div>
	</div>
</body>
<script src="lib/urijs/URI.min.js"></script>
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/qrcode.min.js"></script>
<script src="js/app-common.js"></script>
<script>
	var params = getParams();
	var productId = params.product_id;
	var productName = params.product_name;
	var url;

	$(function() {
		if (!productId) {
			init();
			return;
		}

		if (productName) {
			$(".productName").text(productName);
		}

		$.ajax({
			type: "post",
			url: APP_CONFIG.queryEmployeeUrl,
			data: {
				token: getUserToken()
			},
			success: function(res){
				if (checkServiceResult(res)) {
					var employee = res.data;
					var isOutSourcing = employee.type == 2;

					$(".teamName").text(isOutSourcing ? employee.team.name : '秀域');
					$(".employeeName").text(employee.name);

					var payUrl = URI().directory() + '/pay.html?promoter_id={0}&product_id={1}'.format(employee.user_id, productId);
					url = URI().origin() + '/wxpay/pay.html?url={0}'.format(encodeURIComponent(payUrl));
				}
			},
			complete: function() {
				init(url);
			}
		});
	});

	function init() {
		var errMsg = '此二维码已失效';
		if (!url) {
			$(".productName").text(errMsg);
		}
		createQRCode(url || errMsg);
	}

	function createQRCode(text) {
		var qrcode = new QRCode('qrcode', {
			text: text,
			width: 250,
			height: 250,
			colorDark: '#000000',
			colorLight: '#ffffff',
			correctLevel: QRCode.CorrectLevel.H
		});

		//qrcode.clear();
		//qrcode.makeCode(url);
	}
</script>
</html>
